package com.example.wanandroid.activity.main.home

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.wanandroid.databinding.ItemBannerBinding
import com.example.wanandroid.databinding.ItemBodyBinding
import com.example.wanandroid.databinding.ItemFooterBinding
import com.example.wanandroid.entity.article.Article
import com.example.wanandroid.entity.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

class HomeAdapter : RecyclerView.Adapter<ViewHolder>() {
    private var banners: List<Banner> = ArrayList()
    private var articles: List<Article> = ArrayList()
    private var fragment: Fragment? = null

    fun setBanners(banners: List<Banner>) {
        this.banners = banners
    }

    fun setArticles(articles: List<Article>) {
        this.articles = articles
    }

    fun setFragment(fragment: Fragment){
        this.fragment = fragment
    }


    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> {
                ViewType.banner
            }

            articles.size + 1 -> {
                ViewType.footer
            }

            else -> {
                ViewType.body
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return when (viewType) {
            ViewType.banner -> {
                BannerViewHolder(
                    ItemBannerBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            ViewType.footer -> {
                FooterViewHolder(
                    ItemFooterBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                BodyViewHolder(
                    ItemBodyBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
        }
    }

    override fun getItemCount(): Int {
        return articles.size + 2
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ViewType.banner -> {
                if (banners.isNotEmpty()) {
                    holder as BannerViewHolder
                    holder.binding.banner.setAdapter(object : BannerImageAdapter<Banner>(banners) {
                        override fun onBindView(
                            holder: BannerImageHolder?,
                            data: Banner?,
                            position: Int,
                            size: Int
                        ) {
                            if (holder != null && data != null) {
                                Glide.with(holder.itemView)
                                    .load(data.imagePath)
                                    .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                                    .into(holder.imageView)
                            }
                        }
                    })
                        .addBannerLifecycleObserver(fragment)//添加生命周期观察者
                        .setIndicator(CircleIndicator(fragment!!.requireContext()));
                }
            }

            ViewType.footer -> {
                holder as FooterViewHolder
                holder.binding
            }

            else -> {
                if (articles.isNotEmpty()) {
                    holder as BodyViewHolder
                    holder.binding.title.text = articles[position - 1].title
                }
            }
        }
    }

    object ViewType {
        const val banner = 0
        const val body = 1
        const val footer = 2
    }

    class BannerViewHolder(val binding: ItemBannerBinding) : ViewHolder(binding.root)

    class BodyViewHolder(val binding: ItemBodyBinding) : ViewHolder(binding.root)

    class FooterViewHolder(val binding: ItemFooterBinding) : ViewHolder(binding.root)

}